//
//  ReadListInfoViewController.m
//  Leisure
//
//  Created by xalo on 16/6/30.
//  Copyright © 2016年 朱旭东. All rights reserved.
//

#import "ReadListInfoViewController.h"

@interface ReadListInfoViewController ()<UIWebViewDelegate>
@property(nonatomic,strong)UIWebView *webView;
@property(nonatomic,strong)NSString *html;
@end

@implementation ReadListInfoViewController


- (UIWebView *)webView
{
    if (!_webView) {
        _webView =[[UIWebView  alloc] initWithFrame:self.view.frame] ;
        _webView.delegate = self;
        _webView.scrollView.bounces = NO;
        _webView.scrollView.bouncesZoom = NO;
    }
    return _webView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.webView];
    

    
    //请求数据
    [RequestManager  fetchDataWithURL:kReadListInfoUrl parameters:@{@"contentid":self.readListModel.ID} method:@"POST" complet:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

        //根据<pre>去分隔
        NSArray *array = [dic[@"data"][@"html"] componentsSeparatedByString:@"<pre>"];
        [self.webView  loadHTMLString:array[0] baseURL:nil];
        self.html = array[0];
        
    } updataUI:^{
        
    }];
    
//    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL  URLWithString:@"http://www.baidu.com"]]];
    
}
//收藏事件
- (void)itemAcion{

        [[CoreDataManager defaultManager] insertCollectionMmodelWothModel:self.readListModel html:self.html];

   
}
#pragma mark -------UIWebViewDelegate

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSLog(@"加载完成");

    //正则表达式，屏幕自适应
        NSString *js = @"function imgAutoFit() { \
        var imgs = document.getElementsByTagName('img'); \
        for (var i = 0; i < imgs.length; ++i) {\
        var img = imgs[i];   \
        img.style.maxWidth = %f;   \
        } \
        }";
        js = [NSString stringWithFormat:js, [UIScreen mainScreen].bounds.size.width - 20];
        
        [webView stringByEvaluatingJavaScriptFromString:js];
        [webView stringByEvaluatingJavaScriptFromString:@"imgAutoFit()"];

        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(itemAcion)];
}

- (void)webViewDidStartLoad:(UIWebView *)webView{
    NSLog(@"开始加载");
}

@end
